package com.hyc.myvideo.data.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.api.model.HomeClass;
import com.hyc.myvideo.data.api.model.HomeCourse;
import com.hyc.myvideo.ui.activity.home.CourseActivity;
import com.hyc.myvideo.utils.imguitl.ImgUitl;
import java.util.List;

/**
 * 首页的Adapter
 * Created by dfeads on 2017/8/4.
 */

public class HomeClassExpandableListAdapter extends BaseExpandableListAdapter {
  private Context context;
  private List<HomeClass> group;
  private LayoutInflater inflater;

  public HomeClassExpandableListAdapter(Context context, List<HomeClass> group) {
    this.context = context;
    this.group = group;
    inflater = LayoutInflater.from(context);
  }

  @Override public int getGroupCount() {
    return group == null ? 0 : group.size();
  }

  @Override public int getChildrenCount(int groupPosition) {
    HomeClass homeClass = group.get(groupPosition);
    if (homeClass == null) {
      return 0;
    }
    List<HomeCourse> child = homeClass.getList();
    return child == null ? 0 : child.size();
  }

  @Override public Object getGroup(int i) {
    return null;
  }

  @Override public Object getChild(int i, int i1) {
    return null;
  }

  @Override public long getGroupId(int i) {
    return 0;
  }

  @Override public long getChildId(int i, int i1) {
    return 0;
  }

  @Override public boolean hasStableIds() {
    return false;
  }

  @Override public boolean isChildSelectable(int i, int i1) {
    return true;
  }

  @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
      ViewGroup viewGroup) {
    HomeGroupHolder holder;
    if (convertView == null) {
      convertView = inflater.inflate(R.layout.item_adapter_home_group, viewGroup, false);
      holder = new HomeGroupHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (HomeGroupHolder) convertView.getTag();
    }
    final HomeClass homeClass = group.get(groupPosition);
    if (homeClass != null) {
      holder.mTitle.setText(homeClass.getCurriculum_class_title());
      holder.mMore.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View view) {
          context.startActivity(new Intent(context, CourseActivity.class).putExtra("id",
              homeClass.getCurriculum_class_id())
              .putExtra("title", homeClass.getCurriculum_class_title()));
        }
      });
    }
    return convertView;
  }

  @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
      View convertView, ViewGroup viewGroup) {
    HomeChildHolder holder;
    if (convertView==null){
      convertView = inflater.inflate(R.layout.item_column, viewGroup, false);
      holder=new HomeChildHolder(convertView);
      convertView.setTag(holder);
    }else {
      holder= (HomeChildHolder) convertView.getTag();
    }
    HomeCourse homeCourse = group.get(groupPosition).getList().get(childPosition);
    if (homeCourse!=null){
      ImgUitl.setImg(homeCourse.getCover(), holder.mImg);

      holder.mText.setText(homeCourse.getDescribe());
      holder.mTime.setText(homeCourse.getCurriculum_people_count()+"人已练习");
      //0=免费，1=vip,2=收费
      switch (homeCourse.getCurriculum_charge_state()){
        case 0:
          holder.mTvTip.setVisibility(View.GONE);
          break;
        case 1:
          holder.mTvTip.setText("VIP");
          holder.mTvTip.setBackgroundResource(R.drawable.item_home_tip_vip);
          holder.mTvTip.setVisibility(View.VISIBLE);
          break;
        case 2:
          holder.mTvTip.setText("付费");
          holder.mTvTip.setBackgroundResource(R.drawable.item_home_tip_pay);
          holder.mTvTip.setVisibility(View.VISIBLE);
          break;
      }

      if (homeCourse.getState()==2) {
        //1=视频,2=音频,3=图文
        switch (homeCourse.getCurriculum_hour_type()){
          case 1:
            holder.mTip.setImageResource(R.mipmap.home_video);
            break;
          case 2:
            holder.mTip.setImageResource(R.mipmap.home_audio);
            break;
          case 3:
            holder.mTip.setImageResource(R.mipmap.home_img_text);
            break;
        }
        holder.mTitle.setText(homeCourse.getTitle());
        holder.mTip.setVisibility(View.VISIBLE);
      } else {
        holder.mTitle.setText(
            Html.fromHtml(homeCourse.getTitle() + "<font color='#00D5E2'> · 系列</font>"));
        holder.mTip.setVisibility(View.GONE);
      }
    }
    return convertView;
  }

  class HomeGroupHolder {
    public HomeGroupHolder(View view) {
      ButterKnife.bind(this, view);
    }
    @BindView(R.id.item_adapter_home_group_title) TextView mTitle;
    @BindView(R.id.item_adapter_home_group_more) TextView mMore;
  }

  class HomeChildHolder{
    public HomeChildHolder(View view) {
      ButterKnife.bind(this,view);
    }
    /** 封面 */
    @BindView(R.id.item_column_img) ImageView mImg;
    /** 音频/视频/图文 标志 */
    @BindView(R.id.item_column_img_tip) ImageView mTip;
    /** 标题 */
    @BindView(R.id.item_column_title) TextView mTitle;
    /** 简介 */
    @BindView(R.id.item_column_text) TextView mText;
    /** 练习过的人数 */
    @BindView(R.id.item_column_time) TextView mTime;
    /** 免费/VIP/付费 标志 */
    @BindView(R.id.item_column_tv_tip) TextView mTvTip;
  }
}
